package cn.com.yusys.yusp.admin.service;

import cn.com.yusys.yusp.admin.domain.AdminSmUserMgrOrg;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmUserMgrOrgMapper;
import cn.com.yusys.yusp.commons.mapper.CommonMapper;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.security.SecurityUtils;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.util.CommonUtil;
import cn.com.yusys.yusp.commons.util.DateUtil;

import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @项目名称: yusp-admin
 * @类名称: AdminSmUserMgrOrgService
 * @类描述: 用户授权机构管理service
 * @功能描述: 
 * @创建人: hujun3@yusys.com.cn
 * @创建时间: 2017-12-27 10:45
 * @修改备注: 
 * @修改记录: 修改时间    修改人员    修改原因
 * -------------------------------------------------------------
 * @version 1.0.0
 * @Copyright (c) 2017宇信科技-版权所有
 */
@Service
public class AdminSmUserMgrOrgService extends CommonService {

	private Logger logger = LoggerFactory.getLogger(AdminSmUserMgrOrgService.class);
	@Autowired
	private AdminSmUserMgrOrgMapper mapper;
	
	@SuppressWarnings("rawtypes")
    @Override
	protected CommonMapper getMapper() {
		return null;
	}
	/**
	 * 
	* @方法名称: queryPage
	* @方法描述: 分页查询
	* @参数与返回说明: 
	* @算法描述:
	 */
	public List<Map<String, Object>> queryPage(QueryModel model){
		// 设置分页查询参数(设置到线程变量中了)
		PageHelper.startPage(model.getPage(), model.getSize());
		List<Map<String, Object>> list=this.mapper.findAllByParam(model);
		PageHelper.clearPage();
		return list;
	}
	/**
	 * 
	* @方法名称: insertInfo
	* @方法描述: 新增授权机构
	* @参数与返回说明: 
	* @算法描述:
	 */
	public Map<String,Object> insertInfo(AdminSmUserMgrOrg pool) {
		Map<String,Object> result=new HashMap<String, Object>();
		Map<String, Object> paramMap=new HashMap<String, Object>();
		paramMap.put("userId", pool.getUserId().toString());
		String[] orgs=pool.getOrgId().split(",");
		List<String> list = new ArrayList<String>();
		for(int i=0;i<orgs.length;i++) {
			list.add(orgs[i]);
		}
		paramMap.put("list",list );
		List<Map<String, Object>> res=mapper.findQueryByParam(paramMap);
		String loginCode = SecurityUtils.getCurrentUserLogin();
		pool.setLastChgDt(DateUtil.formatDateTimeByDef());//最近跟新时间
		pool.setLastChgUsr(loginCode);//最近跟新用户
		if(pool.getRelSts()==null||"".equals(pool.getRelSts())) {
			pool.setRelSts("A");//新增的数据都是待生效的
		}
		if(res.size()==0) {
			String[] orgIds=pool.getOrgId().split(",");
			int orgLength=orgIds.length;
			for(int s=0;s<orgLength;s++) {
				AdminSmUserMgrOrg add=pool;
				if(!"".equals(orgIds[s])) {
					add.setOrgId(orgIds[s]);
					logger.info("New data of user authorized organization: [new user authorized organization: {}] ", add.getOrgId());
					this.insert(getMapper(), add);
				}
			}
			result.put("code", "200");
			result.put("message", CommonUtil.formatMessage("用户：{},授权机构名称为:{}的数据保存成功。", pool.getUserId(), pool.getOrgId()));
		}else {//如果已存在就只新增没有的，跳过已有的数据
			int listSize=res.size();
			String[] orgIds=pool.getOrgId().split(",");
			int orgLenght=orgIds.length;
			boolean flag=false;//判断是否已经存在
			String orgName="";
			for(int s =0;s<orgLenght;s++) {
				for(int i=0;i<listSize;i++) {
					Map<String, Object> map=res.get(i);
					if(orgIds[s].equals(map.get("orgId"))) {
						orgName=orgName+"|"+map.get("orgName");
						flag=true;
						break;
					}else {
						flag=false;
					}
				}
				if(!flag) {
					AdminSmUserMgrOrg add=pool;
					if(!"".equals(orgIds[s])) {
						add.setOrgId(orgIds[s]);
						logger.info("New data of user authorized organization: [new user authorized organization: {}] ", add.getOrgId());
						this.insert(getMapper(), add);
					}
				}
			}
			result.put("code", "2");
			result.put("message", "用户为："+pool.getUserId()+",授权机构名称为:"+pool.getOrgId()+"的数据已经存在。");
		}
		
		return result;
	}
	
	
	/** 
	* @方法名称: updateSts
	* @方法描述: 更新数据状态
	* @参数与返回说明: 
	* @算法描述:
	 */
	public int updateSts(String sts,String id) {
		Map<String, String> paramMap=new HashMap<String, String>();
		String loginCode = SecurityUtils.getCurrentUserLogin();
		paramMap.put("id", id);
		paramMap.put("sts", sts);
		paramMap.put("user", loginCode);
		paramMap.put("data", DateUtil.formatDateTimeByDef());
		int result=this.mapper.updateSts(paramMap);
		return result;
	}
	
	/** 
	* @方法名称: update
	* @方法描述: 修改用户授权机构数据
	* @参数与返回说明: 
	* @算法描述:
	 */
	@Override
	public int update(Object record) {
		String loginCode = SecurityUtils.getCurrentUserLogin();
		AdminSmUserMgrOrg pool=(AdminSmUserMgrOrg)record;
		pool.setLastChgDt(DateUtil.formatDateTimeByDef());//最近跟新时间
		if(pool.getRelSts()==null||"".equals(pool.getRelSts())) {
			pool.setRelSts("A");//新增的数据都是待生效的
		}
		pool.setLastChgUsr(loginCode);
		logger.info("User modified data: [modify user: {}] ", pool.getUserId());
		return this.updateSelective(getMapper(), pool);
	}
	
}
